<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\admin\BaseController;
use app\admin\model\Banner;
use app\admin\model\Program;
use mailer\tp6\Mailer;
use think\Exception;
use think\exception\ValidateException;
use think\facade\Filesystem;
use think\facade\Request;
use think\facade\View;

class Index extends BaseController
{
    public function index()
    {
        return view();
    }
    //网站信息
    public function info()
    {
        if (Request::isPost()){
            $post=Request::post();
            //网站logo上传
            try{
                $files=Request::file();
                validate(['logo'=>'filesize:3145728|fileExt:jpg,png,jpeg,gif'])->check($files);
                $path=Filesystem::putFile('imgs',$files['logo'],'md5');
                $path=str_replace('\\','/',$path);
            }catch (ValidateException $validateException)
            {
                $msg=$validateException->getMessage();
            }catch (\Exception $exception){
                $path='';
            }
            if (isset($msg))        $this->error($msg);
            //当上传图片时修改图片
            if ($path != ''){
                $post['logo']=$path;
            }
            //修改网站信息
            $res=Program::update($post);
            if ($res){
                $this->success('修改成功',url('Index/info'));
            }else{
                $this->error('修改失败');
            }
        }else{
            //查询网站信息
            $list=Program::getProgram();
            View::assign($list->toArray());
            return view();
        }

    }
    //修改密码
    public function pass()
    {
        if (Request::isPost()){
            $post=Request::post();
            $time=time();
            if ($time-session('session_start_time')>60)
            {
                session('emailCode',null);
                session('session_start_time',null);
                $this->error('验证码失效');
            }
            $res=\app\admin\model\Admin::where('id','=',session('userInfo.id'))->update(['pwd'=>md5($post['newpass'])]);
            if ($res){
                session('emailCode',null);
                session('session_start_time',null);
                $this->success('修改成功',url('Login/login'));
            }else{
                $this->error('修改失败');
            }
        }else{
            return  view();
        }
    }
    //轮播图
    public function adv()
    {
        //判断是否添加轮播图
        if (Request::isPost()){
            $post=Request::post();
            //轮播图图片上传
            try{
                $files=Request::file();
                validate(['logo'=>'filesize:10485760|fileExt:png,jpg,gif,jpeg'])->check($files);
                $path=Filesystem::putFile('UploadFiles/banner',$files['logo'],'md5');
                $path=str_replace('\\','/',$path);
            }catch (ValidateException $validateException){
                $msg=$validateException->getMessage();
            }catch (\Exception $exception){
                $path='';
            }
            if (isset($msg))      $this->error($msg);
            //当传图片时添加图片路径
            if ($path != '')       $post['photo']=$path;
            $post['addtime']=time();
            $res=Banner::create($post);
            if ($res){
                $this->success('添加成功',url('Index/adv'));
            }else{
                $this->error('添加失败');
            }
        }else{
            //查询轮播图数据展示
            $list=Banner::field(true)->select();
            View::assign('list',$list);
            return  view();
        }
    }
    //轮播图修改
    public function advEdit()
    {
        //获取轮播图get传参id的值
        $id=Request::get('id');
        if (Request::isPost()){
            $post=Request::post();
            //轮播图修改上传
            try{
                $files=Request::file();
                validate(['logo'=>'filesize:10485760|fileExt:jpg,png,jpeg,gif'])->check($files);
                $path=Filesystem::putFile('UploadFiles/banner',$files['logo'],'md5');
                $path=str_replace('\\','/',$path);
            }catch (ValidateException $validateException){
                $msg=$validateException->getMessage();
            }catch (\Exception $exception){
                $path='';
            }
            if (isset($msg))        $this->error($msg);
            if ($path != '')        $post['photo']=$path;
            $post['id']=$id;
            //轮播图修改
            $res=Banner::update($post);
            if ($res){
                $this->success('修改成功',url('Index/adv'));
            }else{
                $this->error('修改失败');
            }
        }else{
            //当有id时展示该id数据
            if ($id){
                $list=Banner::field(true)->find($id);
                View::assign($list->toArray());
            }
            return view();
        }
    }
    //轮播图删除
    public function advDelete()
    {
        //获取要删除的轮播图的id
        $id=Request::get('id');
        if ($id){
            //删除轮播图
            $res=Banner::destroy($id);
            if ($res){
                $this->success('删除成功',url('Index/adv'));
            }else{
                $this->error('删除失败',url('Index/adv'));
            }
        }
    }
    //ajax邮箱验证码发送
    public function ajaxEmailCode()
    {
        $email=Request::post('email');
        $numCode=mt_rand(0000,9999);
        session('emailCode',$numCode);
        session('session_start_time',time());
        $mailer=Mailer::instance();
        $mailer->from('2436934253@qq.com', 'xdz')
            ->to($email)
            ->subject('找回密码')
            ->text('您的验证码为'.$numCode)
            ->send();
    }
}
